package com.horoscope.astrology.zodiac.signs.free.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zyyoona7.wheel.WheelView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.PreMainActivity
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import kotlinx.android.synthetic.main.fragment_born_time.*


/**
 *  出生时间
 * */
class BornTimeFragment : BaseFragment() {

    private lateinit var mGuideContent: TextView
    private lateinit var mStar: ImageView
    private lateinit var mLinearLayoutReply: LinearLayout
    private lateinit var mReply: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_born_time, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mRoot = view.findViewById(R.id.born_root)
        mStar = view.findViewById(R.id.image_view_constellation)
        mLinearLayoutReply = view.findViewById(R.id.linear_layout_reply)
        mReply = view.findViewById(R.id.text_view_reply_born)
        inVisibilityRoot()
        mGuideContent = view.findViewById(R.id.text_view_born_time)
        val check = view.findViewById<ImageView>(R.id.image_view_check)
        val hour = view.findViewById<WheelView<String>>(R.id.wheel_view_hour)
        val min = view.findViewById<WheelView<String>>(R.id.wheel_view_min)
        val wheelView = view.findViewById<WheelView<String>>(R.id.wheel_view_am)
        val list = ArrayList<String>()
        for (i in 1..12) {
            list.add("$i")
        }
        setWheelView(hour, list)
        hour.textAlign = WheelView.TEXT_ALIGN_LEFT

        val minList = ArrayList<String>()
        for (i in 0..59) {
            if (i < 10) {
                minList.add("0$i")
            } else {
                minList.add("$i")
            }
        }
        setWheelView(min, minList)
        min.textBoundaryMargin = SysUtils.rp(45f).toFloat()
        val timeList = ArrayList<String>()
        timeList.add("AM")
        timeList.add("PM")
        setWheelView(wheelView, timeList)
        wheelView.textAlign = WheelView.TEXT_ALIGN_RIGHT

        hour.selectedItemPosition = SysUtils.getCurrentHour().toInt() - 1
        min.selectedItemPosition = SysUtils.getCurrentMin().toInt()
        wheelView.selectedItemPosition = SysUtils.getAMOrPM()
        check.tag = 0 //
        check.setOnClickListener {
            if (check.tag == 0) {
                check.setImageResource(R.mipmap.born_check)
                check.tag = 1
            } else {
                check.setImageResource(R.mipmap.born_un_check)
                check.tag = 0
            }
        }
        text_view_continue?.setOnClickListener {
            // 点击了出生时间
//            FlurryEventUtil.putDataToEvent(EventType.TYPE_SPLASH_TIME, null, null)
            val h = list[hour.selectedItemPosition]
            val m = minList[min.selectedItemPosition]
            val t = timeList[wheelView.selectedItemPosition]
            if (check.tag == 0) {
                (activity as PreMainActivity).setBornTime("$h:$m$t")
                val str = "$h:$m $t"
                disAppearance(str)
            } else {
                disAppearance("I don\'t know")
            }
        }
    }

    private fun disAppearance(str: String) {
        linear_select.visibility = View.INVISIBLE
        linear_check.visibility = View.INVISIBLE
        text_view_continue.visibility = View.INVISIBLE

        val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
        linear_select?.startAnimation(anim)
        linear_check?.startAnimation(anim)
        text_view_continue.startAnimation(anim)
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                fadeInReply(str)
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })

    }

    private fun fadeInReply(str: String) {
        val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_in)
        mLinearLayoutReply.startAnimation(anim)
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                mLinearLayoutReply.postDelayed({
                    fadeOut()
                }, 500)

            }

            override fun onAnimationStart(animation: Animation?) {
                mReply.text = str
            }

        })
    }

    fun fadeOut() {
        val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
        mRoot?.startAnimation(anim)
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                nextPage()
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }


    private fun setWheelView(wheelView: WheelView<String>, list: ArrayList<String>) {
        wheelView.data = list
        wheelView.normalItemTextColor = ContextCompat.getColor(activity, R.color.white)
        wheelView.selectedItemTextColor = ContextCompat.getColor(activity, R.color.white)
        wheelView.dividerColor = ContextCompat.getColor(activity, R.color.gray_3)
        wheelView.textSize = SysUtils.rp(19f).toFloat()
        wheelView.isShowDivider = true
        wheelView.lineSpacing = SysUtils.rp(10f).toFloat()
        wheelView.textBoundaryMargin = SysUtils.rp(5f).toFloat()
        wheelView.dividerHeight = SysUtils.rp(1f).toFloat()
        wheelView.selectedItemPosition = 1
    }

    private var mName = ""

    fun setName(name: String) {
        if (isAdded) {
            mName = name
            setGuideContent(mGuideContent, name, getString(R.string.question_born))
        }
    }


    private fun setGuideContent(textView: TextView?, name: String, str: String) {
        if (!SysUtils.isContainChinese(mName)) {
            textView?.text = "$mName! $str"
        } else {
            val byteSize = name.toByteArray().size
            val newByte = ByteArray(18)
            if (byteSize > 18) {
                for (i in 0..17) {
                    newByte[i] = name.toByteArray()[i]
                }
                textView?.text = String(newByte) + "...! " + str
            } else {
                textView?.text = "$name! $str"
            }
        }
    }

    fun nextPage() {
        (activity as PreMainActivity).setPage(4)
    }


    fun fadeIn() {
        super.fadeInRoot(mRoot)
    }

    fun setConstellationName(name: String) {
        mStar.setImageResource(ConstellationUtil.getIconByName(name))
    }

}